package xyz.xuminghai.javafx_learn.demo.login_demo;

import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.util.Duration;

import java.util.Objects;

/**
 * 2023/11/7 1:57 星期二<br/>
 *
 * @author xuMingHai
 */
public class LoginView extends Pane {

    private final MediaView mediaView = new MediaView();

    private final TextField userTextField = new TextField();

    private final PasswordField passwordField = new PasswordField();

    private final CheckBox rememberMeButton = new CheckBox("记住密码");

    private final Hyperlink forgetPasswordButton = new Hyperlink("忘记密码?");

    private final Button loginButton = new Button("登录");


    public LoginView() {
        initMediaView();
        super.setId("login-view");
        super.getChildren().addAll(mediaView, initLoginPanel());
    }

    private void initMediaView() {
        Media media = new Media(Objects.requireNonNull(LoginView.class.getResource("video.mp4"),
                "video.mp4 没有找到").toExternalForm());
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        // 静音
        mediaPlayer.setMute(true);
        // 媒体应开始播放或重复时重新开始的时间偏移量
        mediaPlayer.setStartTime(Duration.millis(100));
        // 循环播放
        mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
        // 自动播放
        mediaPlayer.setAutoPlay(true);

        // 设置视频形状和位置
        mediaView.setClip(new Circle(640.0, 370.0, 290.0));
        mediaView.setLayoutX(-130.0);
        mediaView.setLayoutY(95.0);
        mediaView.setMediaPlayer(mediaPlayer);
    }

    private Node initLoginPanel() {
        // title
        Text title = new Text("智能科技平台登录页");
        title.getStyleClass().addAll("title", "first-title");
        Text pinYinTitle = new Text("ZHI NENG KE JI PING TAI");
        pinYinTitle.getStyleClass().addAll("title", "last-title");
        VBox titleVBox = new VBox(title, pinYinTitle);
        titleVBox.setId("title-v-box");


        // login-grid-pane
        GridPane loginGridPane = new GridPane();
        loginGridPane.setId("login-grid-pane");

        // userLoginText
//        Text userLoginText= new Text("登录");
        Text userLoginText= new Text("用户登录");
        userLoginText.getStyleClass().add("user-login-text");
        loginGridPane.addRow(0, userLoginText);
        GridPane.setMargin(userLoginText, new Insets(25.0, 0.0, 25.0, 0.0));
        GridPane.setHalignment(userLoginText, HPos.CENTER);
        GridPane.setColumnSpan(userLoginText, 2);

        // usernameHBox
        ImageView usernameImageView = new ImageView();
        usernameImageView.getStyleClass().add("username-image-view");
        userTextField.setPromptText("用户名");
        HBox usernameHBox = new HBox(usernameImageView, userTextField);
        usernameHBox.getStyleClass().add("field-h-box");
        HBox.setMargin(usernameImageView, new Insets(0.0, 15.0, 0.0, 15.0));
        loginGridPane.addRow(1, usernameHBox);
        GridPane.setColumnSpan(usernameHBox, 2);

        // passwordHBox
        ImageView passwordImageView = new ImageView();
        passwordImageView.getStyleClass().add("password-image-view");
        passwordField.setPromptText("密码");
        HBox passwordHBox = new HBox(passwordImageView, passwordField);
        passwordHBox.getStyleClass().add("field-h-box");
        HBox.setMargin(passwordImageView, new Insets(0.0, 15.0, 0.0, 15.0));
        loginGridPane.addRow(2, passwordHBox);
        GridPane.setColumnSpan(passwordHBox, 2);

        // rememberMeButton, forgetPasswordButton
        rememberMeButton.setSelected(true);
        loginGridPane.addRow(3, rememberMeButton, forgetPasswordButton);
        GridPane.setHalignment(forgetPasswordButton, HPos.RIGHT);

        // loginButton
        loginGridPane.addRow(4, loginButton);
        GridPane.setColumnSpan(loginButton, 2);
        GridPane.setMargin(loginButton, new Insets(0.0, 0.0, 25.0, 0.0));

        // border
        Pane topLeftPane = new Pane();
        topLeftPane.setId("top-left-border");
        Pane topRightPane = new Pane();
        topRightPane.setId("top-right-border");
        Pane bottomLeftPane = new Pane();
        bottomLeftPane.setId("bottom-left-border");
        Pane bottomRightPane = new Pane();
        bottomRightPane.setId("bottom-right-border");
        AnchorPane anchorPane = new AnchorPane(topLeftPane, topRightPane, bottomLeftPane, bottomRightPane);

        AnchorPane.setTopAnchor(topLeftPane, 0.0);
        AnchorPane.setLeftAnchor(topLeftPane, 0.0);

        AnchorPane.setTopAnchor(topRightPane, 0.0);
        AnchorPane.setRightAnchor(topRightPane, 0.0);

        AnchorPane.setBottomAnchor(bottomLeftPane, 0.0);
        AnchorPane.setLeftAnchor(bottomLeftPane, 0.0);

        AnchorPane.setBottomAnchor(bottomRightPane, 0.0);
        AnchorPane.setRightAnchor(bottomRightPane, 0.0);

        anchorPane.getChildren().add(loginGridPane);

        // login-panel
        VBox loginPanel = new VBox(titleVBox, anchorPane);
        loginPanel.setId("login-panel");
        return loginPanel;
    }


}
